function loadImg(url){
    let p = new Promise((resolve,reject)=>{
        let img = document.createElement('img');
        img.src = url;
        img.onload = ()=>{
            resolve(img);
        } 
        img.onerror = ()=>{
            reject(new Error('${url} load failed'));
        }
    })
    return p;
}

const url = "https://pic.leetcode-cn.com/1604237471-xbJgZl-%E5%9B%BE%E7%89%871.png";
loadImg(url).then(data=>{
    console.log(data);
}).catch(e=>{
    console.log(e);
})